/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.z100h.supplychain.common.service;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.z100h.supplychain.common.entity.NewResource;
import com.z100h.supplychain.common.entity.RepairResInfoNew;
import com.z100h.supplychain.common.mapper.NewResourceMapper;
import com.z100h.supplychain.common.mapper.RepairResInfoMapper;

/**
 * 
 * [描述信息：说明类的基本功能]
 *
 * @author Elvin Zhao - 279152260@qq.com
 * @version 1.0 Created on 2019年9月26日 下午4:54:48
 */
@Service
@Transactional(readOnly = true)
public class ErpRepairService {

	@Autowired
	private NewResourceMapper newResourceDao;

	@Autowired
	private RepairResInfoMapper repairResInfoDao;

	@Transactional(readOnly = false)
	public void restoreRepairResourceByStockroomId(String stockroomId) {

		List<RepairResInfoNew> repairList = repairResInfoDao.findRepairedResInfoNewByStockroomId(stockroomId);

		if (CollectionUtils.isNotEmpty(repairList)) {

			for (RepairResInfoNew repairRsesInfo : repairList) {

				restoreRepairResource(repairRsesInfo);

			}

		}

	}

	/**
	 * 复壮调换内容恢复逻辑
	 * 
	 * @param repairResInfoNew
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年9月26日 下午4:28:43
	 */
	private void restoreRepairResource(RepairResInfoNew repairResInfoNew) {
		String resId = repairResInfoNew.getResId();
		String oldSpecType = newResourceDao.getSpecTypeById(resId);
		List<NewResource> resourceList = null;
		if (null != oldSpecType && "44".equals(oldSpecType)) {
			resourceList = newResourceDao.findGroupById(resId);
			updateGroupRes(resourceList, repairResInfoNew);
		}
		if (null != oldSpecType && "00".equals(oldSpecType)) {
			resourceList = newResourceDao.findNewResourceById(resId);
			updateRes(resourceList, repairResInfoNew);
		}

	}

	/**
	 * 复壮恢复
	 * 套盆
	 * 
	 * @param resName
	 * @param resourceList
	 * @param repairResInfoNew
	 * @param userId
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年9月26日 下午3:39:47
	 */
	private void updateRes(List<NewResource> resourceList, RepairResInfoNew repairResInfoNew) {

		if (CollectionUtils.isNotEmpty(resourceList)) {
			for (NewResource resId : resourceList) {
				if (resId.getSpecTypeSpecId() == null) {
					repairResInfoNew.setSpecType(resId.getSpecType());
					newResourceDao.updateResourceNew(resId.getId(), repairResInfoNew.getaSpecPlantId(),
							repairResInfoNew);
				} else {
					if (resId.getSpecType().equals("11")) {
						repairResInfoNew.setSpecType(resId.getSpecType());
						newResourceDao.updateResourceNew(resId.getId(), repairResInfoNew.getaSpecFlowerpotId(),
								repairResInfoNew);
					} else {
						repairResInfoNew.setSpecType(resId.getSpecType());
						newResourceDao.updateResourceNew(resId.getId(), repairResInfoNew.getaSpecPlantId(),
								repairResInfoNew);
					}
				}
			}
		}
	}

	/**
	 * 复壮恢复
	 * 组景-盆栽
	 * 
	 * @param resName
	 * @param resourceList
	 * @param repairResInfoNew
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年7月9日 下午5:59:46
	 */
	private void updateGroupRes(List<NewResource> resourceList, RepairResInfoNew repairResInfoNew) {
		if (CollectionUtils.isNotEmpty(resourceList)) {
			for (NewResource resId : resourceList) {
				repairResInfoNew.setSpecType(resId.getSpecType());
				newResourceDao.updateGroupResourceNew(resId.getId(), repairResInfoNew.getaSpecPlantId(),
						repairResInfoNew);
			}
		}
	}

}